/**============================================================================
 *
 * Author: luo1o1o1o 
 *
 * QQ: 330240995, PHONE: 17600071321
 *
 * Last modified: 2021-05-29 20:24
 *
 * Filename: user.js
 *
 * Description: user view 路由
 *
 ============================================================================**/
const router = require('koa-router')()
const { loginRedirect } = require('../../middlewares/loginChecks')

/**
 * @Author: luo1o1o1o
 * @QQ: 330240995, @PHONE: 17600071321
 * @Date: 2021-06-10 00:26
 * @desc: 获取登录信息
 */
function getLoginInfo(ctx) {
    let data = {
        isLogin: false, // 默认未登录
    }
    const userInfo = ctx.session.userInfo
    if (userInfo) {
        data = {
            isLogin: true,
            userName: userInfo.userName,
        }
    }
    return data
}

// 登录
router.get('/login', async (ctx, next) => {
    await ctx.render('login', getLoginInfo(ctx))
})

// 注册
router.get('/register', async (ctx, next) => {
    await ctx.render('register', getLoginInfo(ctx))
})

//
router.get('/setting', loginRedirect, async (ctx, next) => {
    await ctx.render('setting', ctx.session.userInfo)
})

module.exports = router
